package com.vecharm.app.bean

import android.graphics.Point
import com.google.gson.annotations.SerializedName
import com.ling.sdk.http.ApiResponseBean
import com.ling.sdk.utils.Cache

class GoodsBean : ApiResponseBean() {


    @SerializedName("version")
    var goodsVersion = 0


    @SerializedName("cloudShelfBackgroundColor")
    var cloudShelfBackgroundColor: String? = null

    @SerializedName("cloudShelfBackground")
    var cloudShelfBackground: String? = null

    @SerializedName("goodsShelfBackgroundColor")
    var goodsShelfBackgroundColor: String? = null

    @SerializedName("goodsShelfBackground")
    var goodsShelfBackground: String? = null

    @SerializedName("goods")
    var goods: ArrayList<Goods>? = null

    @SerializedName("brands")
    var brands: ArrayList<Brand>? = null

    @SerializedName("checkUpdate")
    var checkUpdate = false


    fun save() {
        if (goodsVersion > 0) {
            goodsBean = this
            Cache.put(TAG, this)
        }
    }

    companion object {

        const val TAG = "goods"

        private var goodsBean: GoodsBean? = null

        fun getGoods(): GoodsBean? {
            if (goodsBean == null)
                goodsBean = Cache.get(TAG, null)
            return goodsBean
        }

        fun getConfig(): GoodsBean {
            return goodsBean ?: Cache.get(TAG, GoodsBean())
        }

        fun findListByBrandId(id: String?): List<Goods>? {
            id ?: return null
            return goodsBean?.goods?.filter { it.brandId == id }
        }

        fun getList(): ArrayList<Goods> {

            val list = ArrayList<Goods>()

            goodsBean?.goods?.forEachIndexed { index, it -> list.add(it) }
            return list
        }

        fun getBrandSize(bean: Brand) = parseSize(bean.brandSize)

        fun getGoodsSize(bean: Goods) = parseSize(bean.goodsSize)

        private fun parseSize(size: String): Point {
            val sizeInt = size.toLowerCase().split("x").map { it.toInt() }
            return Point(sizeInt.getOrNull(0) ?: 180, sizeInt.getOrNull(1) ?: 100)
        }

    }
}